package com.tqz.rc.spring.cloud.ribbon.rule;

import com.netflix.loadbalancer.IRule;
import com.tqz.rc.spring.cloud.TqzRcProperties;
import com.tqz.rc.spring.cloud.TqzRcServiceManager;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>负载均衡自动配置类
 *
 * @author tianqingzhao
 * @since 2021/9/28 17:57
 */
@Configuration
@ConditionalOnClass(TqzRcProperties.class)
public class RuleAutoConfiguration {

    // 权重
    @Bean
    public IRule weightedRule(TqzRcServiceManager serviceManager, TqzRcProperties properties) {
        return new TqzWeightedRule(serviceManager, properties);
    }

    // 通集群优先调用，不存在就调用其他集群
    @Bean
    public IRule theSameClusterPriorityRule(TqzRcServiceManager serviceManager, TqzRcProperties properties) {
        return new TqzRcTheSameClusterPriorityRule(serviceManager, properties);
    }

    // 根据相同版本进行优先调用
    @Bean
    public IRule theSameClusterPriorityWithVersionRule(TqzRcServiceManager serviceManager, TqzRcProperties properties) {
        return new TqzRcTheSameClusterPriorityWithVersionRule(serviceManager, properties);
    }
}
